package pl.controller.add;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import pl.common.SessionDataBean;
import pl.common.Util;
import pl.model.dao.AuthorDao;
import pl.model.dao.BookDao;
import pl.model.domain.Author;
import pl.model.domain.Book;


public class AddBookController extends SimpleFormController implements org.springframework.web.servlet.mvc.Controller{

	protected final Log log = LogFactory.getLog(getClass());
    protected BookDao bookDao;
    protected AuthorDao authorDao;
    
	@Override
	protected ModelAndView onSubmit(Object command) throws Exception {
		// TODO Auto-generated method stub
	//	return super.onSubmit(command);
		AddBookBackingObject abbo = (AddBookBackingObject) command;
		log.debug("on submit start, captcha: " + abbo.getCaptcha());
		log.error("todo: verify captcha");
		try {
			bookDao.merge(abbo.getBook());
			abbo.getSessionData().setCaptcha(null);
			log.debug("book cuccessfully persisted: " + command);
			return new ModelAndView( new RedirectView(getSuccessView()));
		} catch (Exception e){
			e.printStackTrace();
			log.debug("error in persisting book: " + command);
			return new ModelAndView(getFormView());
		}
	}

	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map<Object, Object> data = new HashMap<Object, Object>();
		
		data.put("authors", authorDao.getAll());
		log.error("referenceData returning authors");
		return data;
		// return super.referenceData(request);
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
	//	return super.formBackingObject(request);
		log.error("formBackingObject returning new book");
		
HttpSession session = request.getSession();
    	
    	SessionDataBean data = (SessionDataBean) session.getAttribute("sessionData");
    	
    	if (data == null){
    		data = new SessionDataBean();
    		session.setAttribute("sessionData", data);
    	}
    	if (data.getCaptcha() == null)
    		data.setCaptcha(Util.randomCaptcha());
    	AddBookBackingObject abbo = new AddBookBackingObject();
    	data.setAbbo(abbo);
    	log.debug("set captcha: " + data.getCaptcha());
		return abbo;
	}

	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.setDisallowedFields(new String []{"author"});
		AddBookBackingObject abbo = (AddBookBackingObject) binder.getTarget();
		Book book = abbo.getBook();
		super.initBinder(request, binder);
		log.error("initBinder will set author");
		Integer authorId = null;
		try {
			try{
				authorId = new Integer(request.getParameter("author"));
			} catch (Exception e){}
			if (authorId != null){
				Author author = authorDao.find(authorId);
				book.setAuthor(author);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("something went bad while setting author");
		}
	}

	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}

	public void setAuthorDao(AuthorDao authorDao) {
		this.authorDao = authorDao;
	}
}
